package com.example.online_exam_system.controller;

import com.alibaba.fastjson.JSON;
import com.example.online_exam_system.commons.Result;
import com.example.online_exam_system.dto.Page;
import com.example.online_exam_system.dto.QuestionsDto;
import com.example.online_exam_system.dto.TeacherregDto;
import com.example.online_exam_system.logic.IStudentLogicService;
import com.example.online_exam_system.logic.ITeacherLogicService;
import com.example.online_exam_system.service.TestQuestionsService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lianglong
 * @create 2021-09-27-14:44
 * @describe 登录注册控制器
 */
@ApiOperation("登录注册验证")
@RestController
public class LoginRegCheck {

    @Autowired
    HttpSession session;

    @Autowired
    ITeacherLogicService teacherLogicService;

    @Autowired
    IStudentLogicService studentLogicService;
    @Autowired
    TestQuestionsService testQuestionsService;

    /**
     * @param
     * @return
     * @description 学生登录验证
     * @author lianglong
     */
    @PostMapping("/logincheck")
    public Result loginCheck(@RequestBody Map<String, Object> inParam) {
        return studentLogicService.loginCheck(inParam);
    }

    /**
     * @param
     * @return
     * @description 学生注册验证
     * @author lianglong
     */
    @ApiOperation("学生注册验证")
    @PostMapping("/regcheck")
    public Result regCheck(@RequestBody Map<String, Object> inParam) {
        return studentLogicService.regCheck(inParam);
    }

    @ApiOperation("测试方法")
    @GetMapping("/test")
    public Map<String, Object> test(String id){
        Map<String, Object> questionsByQuestionId = testQuestionsService.getQuestionsByQuestionId(id);
        return questionsByQuestionId;
    }
    /**
     * @description 教师登录验证
     * @param
     * @return
     * @author lianglong
     */
    @PostMapping("/teacherlogincheck")
    public Result teacherLogincheck(@RequestBody Map<String, Object> inParam) {
        return teacherLogicService.teacherLogincheck(inParam);
    }

    /**
     * @description 教师注册验证
     * @param
     * @return
     * @author lianglong
     */
    @PostMapping("teacherregcheck")
    public Result teacherRegCheck(@RequestBody TeacherregDto teacherregDto) {
        return teacherLogicService.teacherRegCheck(teacherregDto);
    }

    @RequestMapping("/initindex")
    public Map<String, Object> init() {
        //获取身份
        String identify = (String) session.getAttribute("identify");
        //学生身份
        if (identify.equals("student")) {
            //首页
            Page homeinfo = new Page("首页", "page/welcome-1", "", "_self");
            Page logoInfo = new Page("ONLINE_EXAM", "/layuimoduls/images/logo.png", "");

            List<Map<String, Object>> stuList = new ArrayList<>();
            List<Map<String,Object>> indexpage = new ArrayList<>();
            indexpage.add(JSON.parseObject(JSON.toJSONString(homeinfo), Map.class));
            //个人信息界面
            Page querystuInfo = new Page("查看信息", "page/querystuInfo", "fa fa-navicon", "_self");
            Page modifystuInfo = new Page("修改信息", "page/modifystuInfo", "fa fa-navicon", "_self");
            List<Map<String,Object>> stuInfo = new ArrayList<>();
            stuInfo.add(JSON.parseObject(JSON.toJSONString(querystuInfo), Map.class));
            stuInfo.add(JSON.parseObject(JSON.toJSONString(modifystuInfo), Map.class));
            Page studentInfo = new Page("个人信息", "", "fa fa-navicon", "",stuInfo);
            //其他界面
            Page mainpage = new Page("主页", "", "fa fa-tachometer", "",indexpage);
            Page studentexam = new Page("参加考试", "page/stu_exam", "fa fa-file-text", "_self");
            Page queryscore = new Page("成绩查询", "page/queryscore", "fa fa-list-alt", "_self");
            //将实体类转为map
            Map mainpage1 = JSON.parseObject(JSON.toJSONString(mainpage), Map.class);
            Map studentexam1 = JSON.parseObject(JSON.toJSONString(studentexam), Map.class);
            Map queryscore1 = JSON.parseObject(JSON.toJSONString(queryscore), Map.class);
            Map studentInfo1 = JSON.parseObject(JSON.toJSONString(studentInfo), Map.class);
            stuList.add(mainpage1);
            stuList.add(studentexam1);
            stuList.add(queryscore1);
            stuList.add(studentInfo1);
            Page menuInfo = new Page("学生界面", "", "fa fa-address-book", "_self", stuList);
            List<Page> list = new ArrayList<>();
            list.add(menuInfo);
            Map<String, Object> init = new HashMap<>();
            init.put("homeInfo", homeinfo);
            init.put("logoInfo", logoInfo);
            init.put("menuInfo", list);
            return init;
        } else if(identify .equals("teacher")){
            Page homeinfo = new Page("首页", "page/welcome-1", "", "_self");
            Page logoInfo = new Page("ONLINE_EXAM", "/layuimoduls/images/logo.png", "");
            List<Map<String, Object>> childs = new ArrayList<>();
            Map<String,Object> map5 =  JSON.parseObject(JSON.toJSONString(homeinfo), Map.class);
            List<Map<String,Object>> homelist = new ArrayList<>();
            homelist.add(map5);
            Page mainpage = new Page("主页", "", "fa fa-tachometer", "",homelist);
            Page queryexam = new Page("查看试卷", "page/queryexams", "fa fa-file-text", "_self");
            Page addexam = new Page("添加试卷","page/addexam","fa fa-file-text","_self");
            Page readpage = new Page("批阅试卷","page/readpage","fa fa-navicon","_self");
            Page queryquestions = new Page("查看试题", "page/queryquestions", "fa fa-list-alt", "_self");
            Page queryscore = new Page("查看成绩", "page/queryscore1", "fa fa-navicon", "_self");
            Page teacherinfo2 = new Page("查看信息","page/teacherinfo", "fa fa-dashboard", "_self");
            Page modifyteacherInfo = new Page("修改信息","page/modifyteacherInfo","fa fa-dashboard","_self");
            Map<String,Object> map = JSON.parseObject(JSON.toJSONString(modifyteacherInfo), Map.class);
            Map<String,Object> map2 = JSON.parseObject(JSON.toJSONString(teacherinfo2), Map.class);
            List<Map<String,Object>> teacherinfolist = new ArrayList<>();
            teacherinfolist.add(map2);
            teacherinfolist.add(map);
            Page teacherinfo = new Page("个人信息", "", "fa fa-dashboard", "_self",teacherinfolist);
            Map<String,Object> map3 = JSON.parseObject(JSON.toJSONString(queryexam), Map.class);
            Map<String,Object> map4 = JSON.parseObject(JSON.toJSONString(addexam), Map.class);
            Map<String,Object> map6 = JSON.parseObject(JSON.toJSONString(readpage), Map.class);
            List<Map<String,Object>> examlist = new ArrayList<>();
            examlist.add(map3);
            examlist.add(map4);
            examlist.add(map6);
            Page examinfo = new Page("试卷管理","","fa fa-file-text","",examlist);
            //将实体类转为map
            Map mainpage1 = JSON.parseObject(JSON.toJSONString(mainpage), Map.class);
            Map examinfo1 = JSON.parseObject(JSON.toJSONString(examinfo), Map.class);
            Map queryquestions1 = JSON.parseObject(JSON.toJSONString(queryquestions), Map.class);
            Map queryscore1 = JSON.parseObject(JSON.toJSONString(queryscore), Map.class);
            Map teacherinfo1 = JSON.parseObject(JSON.toJSONString(teacherinfo), Map.class);

            childs.add(mainpage1);
            childs.add(examinfo1);
            childs.add(queryscore1);
            childs.add(queryquestions1);
            childs.add(teacherinfo1);
            Page menuInfo = new Page("教师界面", "", "fa fa-address-book", "_self", childs);
            List<Page> list = new ArrayList<>();
            list.add(menuInfo);
            Map<String, Object> init = new HashMap<>();
            init.put("homeInfo", homeinfo);
            init.put("logoInfo", logoInfo);
            init.put("menuInfo", list);
            return init;
        }else{
            return null;
        }
    }

}
